import { logger } from '../until/log'
import type TcpClient from '../until/Tcp'
// 1. 初始化这个串口服务
export const initSerialport = (config: Record<string, any>,controller: any) => {
  try {
        let serialName = config.serialName
        let baudRate = parseInt(config.baudRate as any)
        let parity = config.parity
        try {
            controller.connectionSerialport(serialName,baudRate as number,parity)
        } catch(err) {
            logger.error("串口连接参数有问题："+ err)
        }
    } catch(err) {
        logger.error("串口连接参数有问题："+ err)
    }
}

// 2. 初始化这个Tcp服务
export const initSocket = (config: Record<string,any>,tcpClient: TcpClient) => {
  try {
        // 检查是否配置了多个网络地址
        if (config.networkAddresses) {
            const addresses = config.networkAddresses.split(',').map((addr: string) => addr.trim());
            const gateway_name = config.gatewayNames?.split(',').map((name: string) => name.trim())
            tcpClient.connectMultipleServers(addresses, Number(config.port),gateway_name as string[]);
            tcpClient.initCheckGateway()
        } else {
            logger.error("未配置网络地址");
        }
    } catch(err) {
        logger.error("socket连接参数有问题："+ err)
    }
}